package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.pojo.entity.Department;
import com.TeacherInfoServer.pojo.entity.Teacher;
import com.TeacherInfoServer.mapper.DepartmentMapper;
import com.TeacherInfoServer.mapper.TeacherMapper;
import com.TeacherInfoServer.common.service.CommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    public DepartmentMapper departmentMapper;
    @Autowired
    public TeacherMapper teacherMapper;
    @Override
    public Teacher findTeacherByName(String name){
        // 参数校验
        if (name == null || name.trim().isEmpty()) {
            // 参数为空或只包含空白字符，直接返回null
            return null;
        }

        try {
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_name", name.trim()); // 保证查询条件不包含额外空格
            Teacher teacher = teacherMapper.selectOne(queryWrapper);
            return teacher; // 直接返回查询结果，避免了重复查询
        } catch (Exception e) {
            // 异常处理，例如记录日志等
            // log.error("查询教师异常", e);
            return null;
        }

    }

    @Override
    public Department findDepartmentByName(String name){
        try {
            QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name",name);
            return departmentMapper.selectOne(queryWrapper);
        }catch (Exception e){
            return null;
        }

    }
    @Override
    public Department getById(Serializable id){
        Department department= departmentMapper.selectById(id);
        return department;
    }
    @Override
    public Teacher getByTeacherId(String teacherId){
        try {
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_id",teacherId);
            Teacher teacher = teacherMapper.selectOne(queryWrapper);
            return teacher;
        }catch (Exception e){
            return null;
        }

    }



}
